package com.zj.blessings.adapter;

import java.util.ArrayList;
import java.util.List;
import com.zj.blessings.R;
import com.zj.blessings.db.DBLikeItem;
import com.zj.blessings.util.StringUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class LikeItemAdapter extends BaseAdapter {
	private List<DBLikeItem> myLikeItems = new ArrayList<DBLikeItem>();
	private Context mContext;
	private int[] mRes = { R.drawable.ic_item_1, R.drawable.ic_item_2,
			R.drawable.ic_item_3, R.drawable.ic_item_4, R.drawable.ic_item_5 };

	public LikeItemAdapter(Context context) {
		this.mContext = context;
	}

	public void setMyLikeItems(List<DBLikeItem> objects) {
		myLikeItems.clear();
		if (objects != null) {
			for (DBLikeItem object : objects) {
				myLikeItems.add(object);
			}
		}
		notifyDataSetChanged();
	}

	public void addMyLikeItems(List<DBLikeItem> objects) {
		if (objects != null) {
			for (DBLikeItem object : objects) {
				myLikeItems.add(object);
			}
		}
		notifyDataSetChanged();
	}

	public void deleteDBLikeItem(DBLikeItem likeItem) {
		if (likeItem != null) {
			for (DBLikeItem object : myLikeItems) {
				if (object.title.equals(likeItem.title)) {
					myLikeItems.remove(object);
					notifyDataSetChanged();
					break;
				}
			}
		}
	}

	@Override
	public int getCount() {
		return myLikeItems.size();
	}

	@Override
	public Object getItem(int position) {
		return myLikeItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_show, null);
			vh.titleTv = (TextView) convertView.findViewById(R.id.title);
			vh.contentTv = (TextView) convertView.findViewById(R.id.content);
			vh.timeTV = (TextView) convertView.findViewById(R.id.time);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		vh.timeTV.setVisibility(View.VISIBLE);
		convertView.setBackgroundResource(mRes[position % mRes.length]);
		vh.titleTv.setText(myLikeItems.get(position).title);
		vh.contentTv.setText(myLikeItems.get(position).text);
		vh.timeTV.setText(StringUtils.longDate2String2(myLikeItems
				.get(position).save_time));
		return convertView;
	}

	class ViewHolder {
		TextView titleTv;
		TextView contentTv;
		TextView timeTV;
	}
}
